/*
 * Copyright (C) 2011 by ayoung <ayoung.p@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.peaceb.ruletherain;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
//import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/**
 * Display help message
 */
public class AboutActivity extends Activity implements OnClickListener {
    private TextView mFaq;
    private String   mVersion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about);
        
        TextView versionText = (TextView)findViewById(R.id.version);
        Button   emailButton = (Button)findViewById(R.id.send_email);
        
        PackageInfo info;
        try {
            info = getPackageManager().getPackageInfo(getPackageName(), 0);
            mVersion = info.versionName;
            if (C.DEBUG) {
                versionText.setText("This is a test version (" + mVersion + ")");
            }
            else {
                versionText.setText(info.versionName);
            }
        } catch (NameNotFoundException e) {
            Trace.e(this, e.toString());
        }
        
        emailButton.setOnClickListener(this);
        
        mFaq = (TextView)findViewById(R.id.faq);
        
        /* for hyper link */
        mFaq.setMovementMethod(LinkMovementMethod.getInstance());
    }

//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        finish();
//        return super.onTouchEvent(event);
//    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.send_email) {
            sendEmail();
        }
    }
    
    /**
     * send e-mail to developer
     */
    private void sendEmail() {
        String[] mailto = { "ayoung.p@gmail.com" };
        StringBuilder sb = new StringBuilder();
        
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_SUBJECT, "[Rule the Rain " + mVersion + "]");
        intent.setType("text/plain");
        
        intent.putExtra(Intent.EXTRA_EMAIL, mailto);
        
        sb.append("Model Name: " + Util.getModelName() + "\n");
        sb.append("Android: " + Util.getAndroidVersion() + "\n");
        sb.append("Network: " + Util.getNetworkOperatorName(this) + "\n\n");
        sb.append(getText(R.string.device_info_notice));
        intent.putExtra(Intent.EXTRA_TEXT, sb.toString());
        
        startActivity(intent);
    }
}
